/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.amap.AmapUtils;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Receiver;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.ReceiverService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "收货地址接口")
@RestController
@RequestMapping("/member/receiver")
public class ReceiverMemberController extends BaseController {

    @Autowired
    private ReceiverService receiverService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private AmapUtils amapUtils;

    /**
     * 分页查询收货地址
     *
     * @return
    */
    @ApiOperation("分页查询收货地址")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "memberId", value = "会员 id", dataType = "Long", paramType = "query")
    })
    public CommResult<Receiver> list(Long memberId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        Member member = null;
        if (memberId!=null) {
            member = memberService.findById(memberId);
        }

        if (member==null) {
            member = memberService.getCurrent();
        }

        if (member!=null) {
            params.put("memberId",member.getId());
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Receiver> list = receiverService.selectList(params);
        PageResult<Receiver> pageResult = new PageResult<Receiver>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条收货地址
     *
     * @return
    */
    @ApiOperation("查询单条收货地址")
    @GetMapping("/find/{id}")
    public CommResult<Receiver> find(@PathVariable String id) {
        Receiver result = receiverService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("receiver",result);
        return CommResult.success(result);

    }

    /**
     * 保存收货地址
     *
     * @param receiver
     * @return
     */
    @ApiOperation("保存收货地址")
    @PostMapping("/save")
    public CommResult<Receiver> save(@RequestBody Receiver receiver) {
        Member member = memberService.getCurrent();
        receiver.setCreateDate(new Date());
        receiver.setMemberId(member.getId());

        if (receiver.getLat()!=null && receiver.getLng()!=null && receiver.getLat()>0 && receiver.getLng()>0) {
            Map<String, Object> data = amapUtils.geoCode(receiver.getLat(), receiver.getLng());
            if (data.containsKey("areaId")) {
                receiver.setAreaId(Integer.parseInt(data.get("areaId").toString()));
            }
            if (data.containsKey("areaName")) {
                receiver.setAreaName(data.get("areaName").toString());
            }
        }
        int affectCount = receiverService.save(receiver);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Receiver result = receiverService.findById(receiver.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除收货地址
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除收货地址", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除收货地址,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Receiver> del(@PathVariable String ids) {

        receiverService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新收货地址
     *
     * @param receiver
     * @return
     */
    @Log(desc = "更新收货地址", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新收货地址")
    @PostMapping("/update")
    public CommResult<Receiver> update(@RequestBody Receiver receiver) {

        if (receiver.getLat()!=null && receiver.getLng()!=null && receiver.getLat()>0 && receiver.getLng()>0) {
            Map<String, Object> data = amapUtils.geoCode(receiver.getLat(), receiver.getLng());
            if (data.containsKey("areaId")) {
                receiver.setAreaId(Integer.parseInt(data.get("areaId").toString()));
            }
            if (data.containsKey("areaName")) {
                receiver.setAreaName(data.get("areaName").toString());
            }
        }

        int affectCount = receiverService.update(receiver);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Receiver result = receiverService.findById(receiver.getId());
        return CommResult.success(result);
    }

}
